package com.ecoolex.discount.sales.admin.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.DiscountBrandClassify;
import com.ecoolex.discount.sales.entity.DiscountBrandWithBLOBs;
import com.ecoolex.discount.sales.entity.DiscountClassify;
import com.ecoolex.discount.sales.entity.DiscountBrandWithBLOBs.Column;
import com.ecoolex.discount.sales.enums.DiscountBrandStatus;
import com.ecoolex.discount.sales.pojo.AddDiscountBrandClassifyDTO;
import com.ecoolex.discount.sales.pojo.AddDiscountBrandDTO;
import com.ecoolex.discount.sales.pojo.EditDiscountBrandDTO;
import com.ecoolex.discount.sales.pojo.QueryDiscountBrandDTO;
import com.ecoolex.discount.sales.ws.DiscountProductService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Type.Admin,description = "折扣品牌")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
	key = DiscountBrandGraphql.AUTH,
	name = "折扣品牌管理",
	remark = "折扣品牌管理")
public class DiscountBrandGraphql {

	public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-DISCOUNT-BRAND";
	
	@Reference
	private DiscountProductService discountProductService;
	
	@Autowired
	private StorageService storageService;
	
	@TypeField(type = DiscountBrandWithBLOBs.class, field = "logo", description = "折扣品牌Logo")
	public StorageInfo logo(@Source DiscountBrandWithBLOBs source) {
		return storageService.get(source.getLogo());
	}
	
	@TypeField(type = DiscountBrandWithBLOBs.class, field = "image", description = "折扣品牌展示图")
	public StorageInfo image(@Source DiscountBrandWithBLOBs source) {
		return storageService.get(source.getImage());
	}
	
	@TypeField(type = DiscountBrandWithBLOBs.class, field = "discountBrandClassifyList", description = "品牌所属分类列表")
	public List<DiscountBrandClassify> getByBrandId(@Source DiscountBrandWithBLOBs source){
		return discountProductService.getByBrandId(source.getBrandId());
	}
	
	@TypeField(type = DiscountBrandClassify.class, field = "discountClassify", description = "折扣分类")
	public DiscountClassify discountClassify(@Source DiscountBrandClassify source) {
		return discountProductService.getDiscountClassify(source.getClassifyId());
	}
	
	@TypeField(type = DiscountBrandWithBLOBs.class, field = "canOnline", description = "能上线")
	public boolean canOnline(@Source DiscountBrandWithBLOBs source,AdminMemberAuthority user) {
		return user.auth(AUTH_ONLINE) && DiscountBrandStatus.canOnline(source.getStatus());
	}
	
	@TypeField(type = DiscountBrandWithBLOBs.class, field = "canOffline", description = "能下线")
	public boolean canOffline(@Source DiscountBrandWithBLOBs source,AdminMemberAuthority user) {
		return user.auth(AUTH_OFFLINE) && DiscountBrandStatus.canOffline(source.getStatus());
	}
	
	@TypeField(type = DiscountBrandWithBLOBs.class, field = "canEdit", description = "能编辑")
	public boolean canEdit(@Source DiscountBrandWithBLOBs source,AdminMemberAuthority user) {
		return user.auth(AUTH_EDIT);
	}
	
	@TypeField(type = DiscountBrandWithBLOBs.class, field = "canDel", description = "能删除")
	public boolean canDel(@Source DiscountBrandWithBLOBs source,AdminMemberAuthority user) {
		return user.auth(AUTH_DEL) && DiscountBrandStatus.canOnline(source.getStatus());
	}
	
	public static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "discountBrand",description = "折扣品牌详情")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_GET,name = "折扣品牌详情")
	public DiscountBrandWithBLOBs get(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return discountProductService.getDiscountBrand(brandId);
	}
	
	public static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "discountBrandList",description = "折扣品牌列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_QUERY,name = "折扣品牌列表")
	public Pagination<DiscountBrandWithBLOBs> query(@NonNull QueryDiscountBrandDTO query,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return discountProductService.queryDiscountBrand(query,sorts, page);
	}
	
	@MutationField(field = "uploadDiscountBrandLogo",description = "上传折扣品牌Logo")
	public StorageInfo uploadDiscountBrandLogo(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
			user.notAuthority();
		}
		UploadSource uploadSource = new UploadSource("DiscountBrand","Logo");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());
        
        return storageService.upload(request,uploadSource,uploadLimit);
	}
	
	@MutationField(field = "uploadDiscountBrandImage",description = "上传折扣品牌展示图")
	public StorageInfo uploadDiscountBrandImage(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
			user.notAuthority();
		}
		UploadSource uploadSource = new UploadSource("DiscountBrand","Image");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());
        
        return storageService.upload(request,uploadSource,uploadLimit);
	}
	
	public static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addDiscountBrand",description = "添加折扣品牌")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加折扣品牌")
	public DiscountBrandWithBLOBs add(@NonNull AddDiscountBrandDTO record,@NonNull List<AddDiscountBrandClassifyDTO> list,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return discountProductService.addDiscountBrand(record, list, user.userName());
	}
	
	public static final String AUTH_EDIT = AUTH + "-EDIT";
	
	@MutationField(field = "editDiscountBrand",description = "编辑折扣品牌")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑折扣品牌")
	public DiscountBrandWithBLOBs edit(@NonNull EditDiscountBrandDTO record,@NonNull List<AddDiscountBrandClassifyDTO> list,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		DiscountBrandWithBLOBs old = discountProductService.getDiscountBrand(record.getBrandId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "折扣品牌不存在");
		BeanUtil.copy(record,
            old,
            Lists.newArrayList(
            		Column.brandMode,
            		Column.status,
                    Column.deleted,
                    Column.createUser,
                    Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return discountProductService.editDiscountBrand(old, list, user.userName());
	}
	
	public static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "delDiscountBrand",description = "删除折扣品牌")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除折扣品牌")
	public boolean del(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		DiscountBrandWithBLOBs old = discountProductService.getDiscountBrand(brandId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "折扣品牌不存在");
		discountProductService.deleteDiscountBrand(old, user.userName());
		return true;
	} 
	
	public static final String AUTH_ONLINE = AUTH + "-ONLINE";
	
	@MutationField(field = "onlineDiscountBrand",description = "上线折扣品牌")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线折扣品牌")
	public boolean online(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLINE);
		DiscountBrandWithBLOBs old = discountProductService.getDiscountBrand(brandId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "折扣品牌不存在");
		discountProductService.onlineDiscountBrand(old, user.userName());
		return true;
	}
	
	public static final String AUTH_OFFLINE = AUTH + "-OFFLINE";
	
	@MutationField(field = "offlineDiscountBrand",description = "下线折扣品牌")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线折扣品牌")
	public boolean offline(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLINE);
		DiscountBrandWithBLOBs old = discountProductService.getDiscountBrand(brandId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "折扣品牌不存在");
		discountProductService.offlineDiscountBrand(old, user.userName());
		return true;
	}
}
